# !usr/bin/python
# -*-coding: utf-8-*-
# FileName: datastructure.py


# alignment node
class ANODE:
	"""ANODE"""
	Value = None
	Next = None

	def __init__(self, data):
		self.Value = data

# stack node
class SNODE:
	"""ANODE"""
	Value = None
	Next = None

	def __init__(self, data):
		self.Value = data

# single list node
class LSNODE:
	"""SLNODE"""
	Value = None
	Next = None

	def __init__(self, data):
		self.Value = data
		
# doubly list node
class LDNODE:
	"""DLNODE"""
	Value = None
	Next = None
	Pre = None

	def __init__(self, data):
		self.Value = data
		
# tree list node
class TNODE:
	"""TNODE"""
	Value = None
	LNext = None
	RNext = None

	def __init__(self, data):
		self.Value = data

# FIFO
class FIFO_LL:
	"""FIFO_LL"""

	MAXSIZE = 1024

	length = 0
	front = None
	rear = None

	def __init__(self, maxsize=1024):
		self.MAXSIZE = maxsize

	def Set(self, data):
		if(self.length >= self.MAXSIZE):
			return None

		node = ANODE(data)

		if(self.length == 0):
			self.front = node
			self.rear = node
		else:
			self.rear.Next = node
			self.rear = node
		
		self.length += 1
		return self.rear

	def Get(self):
		if(self.length == 0):
			return None
		
		if(self.length == 1):
			tmp = self.front
			self.front = None
			self.rear = None
		else:
			tmp = self.front
			self.front = self.front.Next

		self.length -= 1
		data = tmp.Value
		del(tmp)
		return data

	def isEmpty(self):
		if(self.length == 0):
			return True
		else:
			return False

	def Length(self):
		return self.length

# FIFO test
# a = FIFO();
# for i in [1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1,'a','b','c']:
# 	a.Set( i );
# while( a.isEmpty() != True ):
# 	print( a.Get() )

# STACK
class STACK_LL:
	"""STACK_LL"""

	MAXSIZE = 1024

	length = 0
	top = None
	bottom = None

	def __init__(self, maxsize=1024):
		self.MAXSIZE = maxsize

	def Push(self, data):
		if ( self.length >= self.MAXSIZE ):
			return None

		node = SNODE( data )

		if ( self.length == 0 ):
			self.top = node
			self.bottom = node
		else:
			node.Next = self.top
			self.top = node

		self.length += 1
		return self.top

	def Pop(self):
		if( self.length == 0 ):
			return None

		if( self.length == 1 ): 
			tmp = self.top
			tmp.Next = None
			self.top = None
			self.bottom = None

		else:
			tmp = self.top
			self.top = self.top.Next
			tmp.Next = None

		self.length -= 1
		data = tmp.Value
		del(tmp)
		return data

	def isEmpty(self):
		if( self.length == 0 ):
			return True
		else:
			return False

	def Length(self):
		return self.length

# STACK test
# s = STACK();
# for i in [1,2,3,4,5,6,7,8,9,'a','b','c','d']:
# 	s.Push( i );
# while( s.isEmpty() != True ):
# 	print( s.Pop() )